<template>
  <div class="app-container">
    <el-card class="tree-card">
      <TreeTools
        :tree-node="company"
        :is-root="true"
        @addDepts="addDepts"
      />
      <el-tree :data="departs" :props="defaultProps" default-expand-all>
        <TreeTools
          slot-scope="{ data }"
          :tree-node="data"
          @delDepts="loadDepartment"
          @addDepts="addDepts"
          @editDepts="editDepts"
        />
      </el-tree>
    </el-card>
    <AddDeptDialog
      v-if="showAddDialog"
      ref="addDepts"
      :show-add-dialog="showAddDialog"
      :node="node"
      :close-add-dialog.sync="showAddDialog"
      @submitAddDepts="loadDepartment"
    />
  </div>
</template>

<script>
import TreeTools from './components/tree-tools.vue'
import AddDeptDialog from './components/add-dept.vue'
import { getDepartments } from '@/api/departments'
import { tranListToTreeData } from '@/utils/index'

export default {
  name: '',
  components: { TreeTools, AddDeptDialog },
  props: {},
  data() {
    return {
      company: {
        name: '成名芳旭科技股份有限公司',
        manager: '负责人',
        id: ''
      },
      departs: [],
      defaultProps: {
        label: 'name' // 表示 从这个属性显示内容
      },
      showAddDialog: false,
      node: {}
    }
  },
  computed: {},
  watch: {},
  created() {
    this.loadDepartment()
  },
  mounted() {},
  updated() {},
  methods: {
    async loadDepartment() {
      const data = await getDepartments()
      this.departs = tranListToTreeData(data.depts, '')
    },
    addDepts(node) {
      this.showAddDialog = true
      this.node = node
    },
    async editDepts(node) {
      this.node = node
      this.showAddDialog = true
      await this.$nextTick()
      this.$refs.addDepts.getDeptsDetail(node.id)
    }
  }
}
</script>

<style scoped lang="scss">
.tree-card {
  padding: 30px 100px;
  font-size: 14px;
}
</style>
